(function($){
  var pageData = {};
  var curPage=1;
  /*
  绑定事件
  */
  var bindEvent = function(){

    /*滚动条*/
    var $doc = $(document);
    $doc.scroll(function(){
      //console.log("height = "+($doc.height() - $(window).height() - 20));
      if($doc.scrollTop() >= ($doc.height() - $(window).height() - 20)){
      	//console.log("scrollTop = "+$doc.scrollTop());
      	var $msg = $("#no-data-msg");
      	if(!$.isEmptyObject(pageData) && !pageData.lastPage){
      		if(!$msg.hasClass("am-hide")){
  	    		$msg.addClass("am-hide");
  	    	}
      		var params = {"pageNumber":pageData.pageNumber + 1,"pageSize":10,"storeMasterId":getParamValue("storeMasterId"),"keyword":$('#keyword').val()};
      		if(curPage<pageData.totalPage){
      			curPage++;
      			loadDatas(params);
      		}
      	}else{
      	    	if($msg.hasClass("am-hide")){
      	    		$msg.removeClass("am-hide");
      	    	}	

      	}
      }
    });
    	
  };
  /*
  显示数据
  */
  var showDatas = function(data){
  	var $tpl = $('#amz-tpl');
    var source = $tpl.text();
    var template = Handlebars.compile(source);
    var myData = {
    	list: data.list,
    	"error":"没有通知"
    };

    var html = template(myData);
    $tpl.before(html);
    bindEvent();
    
    var $msg = $("#no-data-msg");
    if(data.list.length==0){
  		if(!$msg.hasClass("am-hide")){
	    		$msg.addClass("am-hide");
	    	}
    }else{
	    	if($msg.hasClass("am-hide")){
  	    		$msg.removeClass("am-hide");
  	    	}   	
    }
  };
  /*
  加载数据
  */
  var loadDatas = function(params){
  	$('#modal').modal('open');
  	
  	console.log("userid:"+getParamValue("storeMasterId"));
  	if(!params)
  	  params = {"pageNumber":1,"pageSize":10,"storeMasterId":getParamValue("storeMasterId"),"keyword":$('#keyword').val()};
 	$.ajax({
	    type: "post",
	    url: "../noticeH5/list",
	    data: params,
	    dataType: "json",
	    headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
	    	$('#modal').modal('close');
	    	//$.AMUI.progress.done();
	    	pageData = data;
	        console.info(data);
	        showDatas(data);
	    },
	    error : function(textStatus) {
	    	//$.AMUI.progress.done();
	    	$('#modal').modal('close');
	        alert("获取通知失败！");
	        console.info("request error");
	    }
	  });
  };
  
  var search = function(params){
	  	$('#modal').modal('open');
	  	if(!params)
	  	  params = {"pageNumber":1,"pageSize":10,"storeMasterId":getParamValue("storeMasterId"),"keyword":$('#keyword').val()};
	 	$.ajax({
		    type: "post",
		    url: "../noticeH5/list",
		    data: params,
		    dataType: "json",
		    headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
		    	$('#modal').modal('close');
		    	//$.AMUI.progress.done();
		    	pageData = data;
		        console.info(data);
		        removeUlContent();
		        showDatas(data);
		    },
		    error : function(textStatus) {
		    	//$.AMUI.progress.done();
		    	$('#modal').modal('close');
		        alert("获取通知失败！");
		        console.info("request error");
		    }
		  });
	  };
  
  var removeUlContent = function(){
	  $("#notice-list > li").remove(); 
	  $("#no-content-msg").remove();
  };
  

  $(function(){
    /*加载模态窗口*/
  	var options = {closeViaDimmer: false};
    $('#modal').modal(options);
    curPage = 1;
  	$('#search_button').click(function() {
  		curPage = 1;
  		$("#keyword").val($("#keyvalue").val());
  		search();
  	});
    loadDatas();
  });
  


})(jQuery);
